package com.example.zcq.rxjava2retrofitmvp.presenter;

import android.util.Log;

import com.example.zcq.rxjava2retrofitmvp.bean.PersonInfo;
import com.example.zcq.rxjava2retrofitmvp.contract.TestContract;
import com.example.zcq.rxjava2retrofitmvp.httpservice.NetWorkRetrofit;
import com.example.zcq.rxjava2retrofitmvp.constant.Constant;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;


/**
 * @author zcq
 * @package com.example.zcq.rxjava2retrofitmvp.presenter
 * @fileName Rxjava2RetrofitMVP
 * @description: 这个类的描述
 * @org 融信通达科技有限公司
 * @email @163.com
 * @date 2018/9/20  11:38
 */
public class TestPresenter extends TestContract.Presenter {

    public TestPresenter(TestContract.View view) {
        super(view);
    }

    @Override
    public void loadData() {
        Map<String, String> map = new HashMap<>();
        map.put("idcard", "100000000000000000");

        NetWorkRetrofit.getSimpleRetrofit().requestPrInfo(map)
                .compose(new NetWorkRetrofit.NetWorkTransformer<PersonInfo>())
                .map(new Function<PersonInfo, String>() {
                    @Override
                    public String apply(PersonInfo personInfo) throws Exception {
                        if (personInfo != null) {
                            return personInfo.getMessage();
                        }
                        return null;
                    }
                })
                .subscribe(new Observer<String>() {
                    private Disposable mDisposable;

                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(String personInfo) {
                        mView.update(personInfo);
                        Log.w(Constant.TAG, "onNext: " + personInfo);
                        mDisposable.dispose();

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.w(Constant.TAG, "onNext: " + e);
                        mDisposable.dispose();//注销


                    }

                    @Override
                    public void onComplete() {

                    }
                });

        /**
         * 不用rxjava2请求数据
         */
      /*  NetWorkRetrofit.getSimpleRetrofit().requestPrInfo2(map).enqueue(new Callback<PersonInfo>() {
            @Override
            public void onResponse(Call<PersonInfo> call, Response<PersonInfo> response) {
                mView.update(response.body().getMessage());
            }

            @Override
            public void onFailure(Call<PersonInfo> call, Throwable t) {

            }
        });*/

    }


}
